<?php

    /**
     * Soapclient
     *
     * @package      Core
     * @subpackage   Soap_Adapter
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Soap_Adapter_Soapclient extends Core_Soap_Adapter_Abstract
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Calls the SOAP server with the requested parameters
         *
         * @param       string      $method
         * @param       array       $params
         * @throws      Core_Exception
         * @throws      Core_ArgumentException
         * @throws      Core_ArgumentNullException
         * @return      array
         */
        public function call($method, array $params = array())
        {
            if ($method === null) {
                throw new Core_ArgumentNullException('method is a null reference');
            }

            if (strlen(trim($method)) == 0) {
                throw new Core_ArgumentException('method is a zero-length string');
            }

            if ($this->_url === null) {
                throw new Core_ArgumentNullException('Url must be implemented in subclasses');
            }

            if (strlen(trim($this->_url)) == 0) {
                throw new Core_ArgumentException('Url must be implemented in subclasses');
            }

            try {
                $client = new SoapClient($this->_url);
                $data   = $client->$method($params);

                return $this->_toArray($data);
            } catch (Exception $e) {
                throw new Core_SoapException($e->getMessage());
            }

            return array();
        }

    ////////////////////////////////////////////////////////////////////////////
    //                             Private Methods                            //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Converts the result data to an array
         *
         * @param       stdClass        $data
         * @return      array
         */
        private function _toArray($data)
        {
            $array = array();

            foreach ($data as $key => $value) {
                if (is_object($value) || is_array($value)) {
                    $array[$key] = $this->_toArray($value);
                } else {
                    $array[$key] = $value;
                }
            }

            return $array;
        }
    }

